package adsf.adminisadsf.gugedianzishichang.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import adsf.adminisadsf.gugedianzishichang.R;

/**
 * Created by administrator on 17/5/9.
 */


/**
 * 根据当前状态来显示不同页面的自定义控件
 *
 * - 未加载 - 加载中 - 加载失败 - 数据为空 - 加载成功
 *
 */

public abstract class LoadingPage extends FrameLayout {


    private static final int STATE_LOAD_UNDO = 1;        //未加载
    private static final int STATE_LOAD_LOADING = 2;     //正在加载
    private static final int STATE_LOAD_ERROR = 3;       // 加载失败
    private static final int STATE_LOAD_EMPTY = 4;       // 数据为空
    private static final int STATE_LOAD_SUCCESS = 5;     // 加载成功

    private int mCurrentState = STATE_LOAD_UNDO;         //当前状态
    private View mLoadingPage;
    private View mErrorPage;
    private View mEmptyPage;
    private View mSuccessPage;


    public LoadingPage(Context context) {
        super(context);
        initView();
    }

    public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public LoadingPage(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    //加载布局
    private void initView()
    {
        //初始化加载中布局
        if (mLoadingPage == null)
        {
            mLoadingPage = UIUtils.inflate(R.layout.page_loading);
            addView(mLoadingPage);
        }

        //加载失败布局
        if (mErrorPage == null)
        {
            mErrorPage = UIUtils.inflate(R.layout.page_error);
            addView(mErrorPage);
        }

        //空布局
        if (mEmptyPage == null)
        {
            mEmptyPage = UIUtils.inflate(R.layout.page_empty);
            addView(mEmptyPage);
        }

        //如果这样写的话三个布局文件会重叠在一起,所有写一个方法加载想要加载的布局

        showRightPage();

    }

    private void showRightPage()
    {
        //如果等于正在未加载或者正在加载的话就显示正在加载    否则隐藏
        mLoadingPage.setVisibility((mCurrentState == STATE_LOAD_UNDO || mCurrentState == STATE_LOAD_LOADING ) ? View.VISIBLE : View.GONE);

        mErrorPage.setVisibility((mCurrentState == STATE_LOAD_ERROR) ? View.VISIBLE : View.GONE);

        mEmptyPage.setVisibility((mCurrentState == STATE_LOAD_EMPTY) ? View.VISIBLE : View.GONE);

        //不能全部都加载三个布局,得有个成功的布局,但是成功的布局什么样子不知道,所以来个抽象方法,让子类去实现就行,那么成功布局为空或者加载成功就显示成功的布局
        //如果当前状态为空并且状态为成功 才初始化成功的布局
        if (mSuccessPage == null && mCurrentState == STATE_LOAD_SUCCESS)
        {
            mSuccessPage = onCreateSuccessView();
            if (mSuccessPage != null)
            {
                addView(mSuccessPage);
            }
        }


        if (mSuccessPage != null)
        {
            mSuccessPage.setVisibility((mCurrentState == STATE_LOAD_SUCCESS) ? View.VISIBLE : View.GONE);

        }


    }


    //开始加载数据
    public void loadData()
    {
        if (mCurrentState != STATE_LOAD_LOADING)  //如果当前没有加载的话 才开始加载数据
        {
            //加载成功以后修改当前状态
            mCurrentState = STATE_LOAD_LOADING;
            //加载数据是网络耗时操作所以开启子线程
            new Thread()
            {
                @Override
                public void run() {
                    super.run();

                    //根据返回的结果加载界面
                    final ResultState resultState = onLoad();

                    //更新UI
                    UIUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {

                            if (resultState != null)
                            {
                                mCurrentState = resultState.getState();

                                //刷新UI  所以得加载主线程
                                showRightPage();
                            }
                        }
                    });



                }
            }.start();
        }
    }



    //加载成功后显示的布局 必须由d调用者来实现
    public abstract View onCreateSuccessView();

    //加载网络数据子类必须实现  返回值表示加载的成功或者失败
    public abstract ResultState onLoad();

    public enum ResultState
    {
        STATE_SUCCESS(STATE_LOAD_SUCCESS),STATE_ERROR(STATE_LOAD_ERROR),STATE_EMPTY(STATE_LOAD_EMPTY);

        //构造方法
        private  int state;
        private ResultState(int state)
        {
            this.state = state;
        }


        public int getState()
        {
            return state;
        }

    }




}
















